package com.hongxun.design.mytools;

import android.content.Context;
import android.content.SharedPreferences;

import com.hongxun.design.MyApp;

/**
 * Created by zerdoor_pc on 2015/10/13.
 * SharedPreferences工具类
 */
public class ShardPreferencesTool {

    public static void saveshare(Context context, String key, String value) {
        try {
            SharedPreferences share = context.getSharedPreferences("dc", Context.MODE_PRIVATE);
            SharedPreferences.Editor edit = share.edit();
            edit.putString(key, value);
            edit.commit();
        } catch (Exception e) {
        }
    }

    public static void saveshare(Context context, String key, int value) {
        try {
            SharedPreferences share = context.getSharedPreferences("dc", Context.MODE_PRIVATE);
            SharedPreferences.Editor edit = share.edit();
            edit.putInt(key, value);
            edit.commit();
        } catch (Exception e) {
        }
    }

    public static void saveshare(Context context, String key, Boolean value) {
        try {
            SharedPreferences share = context.getSharedPreferences("dc", Context.MODE_PRIVATE);
            SharedPreferences.Editor edit = share.edit();
            edit.putBoolean(key, value);
            edit.commit();
        } catch (Exception e) {
        }
    }

    public static String getshare(Context context, String key, String value) {
        try {
            return context.getSharedPreferences("dc", Context.MODE_PRIVATE).getString(key, value);
        } catch (Exception e) {
            return value;
        }

    }

    public static int getshare(Context context, String key, int value) {
        try {
            return context.getSharedPreferences("dc", Context.MODE_PRIVATE).getInt(key, value);
        } catch (Exception e) {
            return value;
        }

    }

    public static Boolean getshare(Context context, String key, Boolean value) {
        try {
            return context.getSharedPreferences("dc", Context.MODE_PRIVATE).getBoolean(key, value);
        } catch (Exception e) {
            return value;
        }

    }

    /**
     * 保存照片数据
     * 0Context
     * 1key
     * 2value
     */
    public static void saverecord(Object... obj) {
        try {
            Context context = (Context) obj[0];
            SharedPreferences share = context.getSharedPreferences("photo", Context.MODE_PRIVATE);
            SharedPreferences.Editor edit = share.edit();
            edit.putString(String.valueOf(obj[1]), String.valueOf(obj[2]));
            edit.commit();
        } catch (Exception e) {
        }
    }

    /**
     * 读取照片数据
     * 0Context
     * 1key
     * 2value
     */
    public static String getrecord(Object... obj) {
        try {
            Context context = (Context) obj[0];
            return context.getSharedPreferences("photo", Context.MODE_PRIVATE).getString(String.valueOf(obj[1]), String.valueOf(obj[2]));
        } catch (Exception e) {
            return String.valueOf(obj[2]);
        }

    }

    /**
     * 删除数据
     * 0Context
     * 1key
     */
    public static boolean delrecord(Object... obj) {
        try {
            Context context = (Context) obj[0];
            SharedPreferences share = context.getSharedPreferences("photo", Context.MODE_PRIVATE);
            SharedPreferences.Editor edit = share.edit();
            edit.remove(String.valueOf(obj[1]));
            return edit.commit();
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 保存数据
     * 0Context
     * 1key
     * 2value
     * 3casse:1,2,3,4...
     */
    public static void save(Object... obj) {
        try {
            Context context = (Context) obj[0];
            int isPro = (int) obj[3];
            String filename = null;
            switch (isPro) {
                case 1://验收基础数据
                    filename = MyApp.userInfo.mobilePhone + "ys";
                    break;
                case 2://验收产生记录数据
                    filename = MyApp.userInfo.mobilePhone + "ys" + MyApp.currProject.getProject_id();
                    break;
                case 3://无网络材料部分数据
                    filename = MyApp.userInfo.mobilePhone + "offline" + MyApp.currProject.getProject_id();
                    break;
            }
            SharedPreferences share = context.getSharedPreferences(filename, Context.MODE_PRIVATE);
            SharedPreferences.Editor edit = share.edit();
            edit.putString(String.valueOf(obj[1]), String.valueOf(obj[2]));
            edit.commit();
        } catch (Exception e) {
        }
    }

    /**
     * 读取数据
     * 0Context
     * 1key
     * 2defValue
     * 3casse:1,2,3,4...
     */
    public static String read(Object... obj) {
        try {
            Context context = (Context) obj[0];
            int isPro = (int) obj[3];
            String filename = null;
            switch (isPro) {
                case 1://验收基础数据
                    filename = MyApp.userInfo.mobilePhone + "ys";
                    break;
                case 2://验收产生记录数据
                    filename = MyApp.userInfo.mobilePhone + "ys" + MyApp.currProject.getProject_id();
                    break;
                case 3://无网络材料部分数据
                    filename = MyApp.userInfo.mobilePhone + "offline" + MyApp.currProject.getProject_id();
                    break;
            }
            SharedPreferences share = context.getSharedPreferences(filename, Context.MODE_PRIVATE);
            String string = share.getString(String.valueOf(obj[1]), String.valueOf(obj[2]));
            return string;
        } catch (Exception e) {
            return String.valueOf(obj[2]);
        }
    }

    /**
     * 删除数据
     * 0Context
     * 1key
     * 2casse:1,2,3,4...
     */
    public static boolean delete(Object... obj) {
        try {
            Context context = (Context) obj[0];
            int isPro = (int) obj[2];
            String filename = null;
            switch (isPro) {
                case 1://验收基础数据
                    filename = MyApp.userInfo.mobilePhone + "ys";
                    break;
                case 2://验收产生记录数据
                    filename = MyApp.userInfo.mobilePhone + "ys" + MyApp.currProject.getProject_id();
                    break;
                case 3://无网络材料部分数据
                    filename = MyApp.userInfo.mobilePhone + "offline" + MyApp.currProject.getProject_id();
                    break;
            }
            SharedPreferences share = context.getSharedPreferences(filename, Context.MODE_PRIVATE);
            SharedPreferences.Editor edit = share.edit();
            edit.remove(String.valueOf(obj[1]));
            return edit.commit();
        } catch (Exception e) {
            return false;
        }
    }
}
